package com.example.demo;
//alt + shift + f

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;


// RestController = Controller(默认返回视图) + ResponseBody(返回数据)
//当有多个注解时,注解没有先后顺序
@RequestMapping("/yzhRequest")
@RestController
public class TestController {

    @RequestMapping("/he")
    public String say() {
        return "hehe";
    }

    @RequestMapping("/r1")
    public String r1(String name) {
        return "接受到参数" + name;
    }

    @RequestMapping("r2")
    public String r2(String age) {
        return "年龄:" + age;
    }


    @RequestMapping("/r4")
    public String r4(String name, Integer age) {
        return "接受到参数:" + name + " " + age;
    }

    @RequestMapping("/r5")
    public Student r5(Student student) {
        return student;
    }

    @RequestMapping("/r6")
    public String r6(@RequestParam("name") String userName) {
        return userName;
    }

    @RequestMapping("/r7")
    public String r7(@RequestParam(value = "name", required = false) String userName) {
        return userName;
    }

    @RequestMapping("/r8")
    public String r8(@RequestParam(value = "name", required = false) String userName) {
        return "asd" + userName;
    }

    @RequestMapping("/r81")
    public String r8() {
        return "dsa";
    }

    @RequestMapping("/r9")
    public String r9(Student student1) {
        return "接受到参数" + student1;
    }

    @RequestMapping("/r10")
    public String r10(@RequestParam(value = "stu", required = false) Student student1) {
        return "接受到参数" + student1;
    }

    @RequestMapping("/r11")
    public String r11(String[] arr) {
        return Arrays.toString(arr);
    }

    @RequestMapping("/m6")
    public String r12(@RequestParam List<String> listParam) {
        return "size:" + listParam.size() + ",listParam:" + listParam;
    }

    //传JSON  RequestBody: 请求正⽂，意思是这个注解作⽤在请求正⽂的数据绑定
    //没有这个注解的话 就不会赋值
    @RequestMapping("/r12")
    public String r12(@RequestBody Student student) {
        return "接收到参数： " + student;
    }

    //从路径中获取参数 ,path variable: 路径变量 ,请求URL路径上的数据绑定
    // article/111  获取到参数: 111
    @RequestMapping("/article/{articleId}")
    public String r13(@PathVariable Integer articleId) {
        return "获取到参数: " + articleId;
    }

    @RequestMapping("/article/{articleId}/{articleName}")
    public String r14(@PathVariable Integer articleId, @PathVariable("articleName") String use) {
        return "获取到参数: " + articleId + use;
    }
    // article/111/yzh  获取到参数: 111yzh

    @RequestMapping("/r15")
    public String r15(@RequestPart("file") MultipartFile imgFile) {
        String originalFilename = imgFile.getOriginalFilename();
        return originalFilename;
    }
}

